﻿angular.module('azcl.common.directive', [])
    .filter("TransYN", function () {
        return function (input) {
            if (input == 'Y')
                return '是';

            if (input == 'N')
                return '否';

            return input;
        }
    })
    .directive('adLoading', ['$http', function ($http) {
        // 增加loading指令, 当http加载时显示，加载完成隐藏
        return {
            restrict: 'A',
            link: function (scope, elm, attrs) {
                scope.isLoading = function () {
                    return $http.pendingRequests.length > 0;
                };

                scope.$watch(scope.isLoading, function (v) {
                    if (v) {
                        elm.show();
                    } else {
                        elm.hide();
                    }
                });
            }
        };
    }])
    .directive('adNotLoading', ['$http', function ($http) {
        // 增加notLoading指令, 当http加载时不显示，加载完成后显示
        return {
            restrict: 'A',
            link: function (scope, elm, attrs) {
                scope.isLoading = function () {
                    return $http.pendingRequests.length > 0;
                };

                scope.$watch(scope.isLoading, function (v) {
                    if (v) {
                        elm.hide();
                    } else {
                        elm.show();
                    }
                });
            }
        };
    }])
    .directive('adDate', function () {
        // 日期校验
        return {
            restrict: 'A',
            require: 'ngModel',
            link: function (scope, elm, attrs, ngModel) {
                var DATE_REGEXP = /^(\d{4})(-|\/)(\d{1,2})\2(\d{1,2})$/;
                var DATE_REGEXP2 = /^(\d{4})(\d{2})(\d{2})$/;

                ngModel.$parsers.push(function (viewValue) {
                    var valid = true;
                    if (!ngModel.$isEmpty(viewValue)) {
                        var r = viewValue.match(DATE_REGEXP);
                        var sy = null;
                        var sm = null;
                        var sd = null;
                        if (r != null) {
                            sy = r[1];
                            sm = r[3];
                            sd = r[4];
                        } else {
                            r = viewValue.match(DATE_REGEXP2);
                            if (r != null) {
                                sy = r[1];
                                sm = r[2];
                                sd = r[3];
                            }
                        }

                        if (r != null) {
                            var newDate = new Date(sy, sm - 1, sd);
                            var y = newDate.getFullYear();
                            var m = newDate.getMonth() + 1;
                            var d = newDate.getDate();
                            valid = (y == sy && m == sm && d == sd);

                            if (valid) {
                                var newViewValue = y + '-' + m + '-' + d;
                                if (newViewValue != viewValue) {
                                    ngModel.$setViewValue(newViewValue);
                                    ngModel.$render();
                                }
                            }
                        } else {
                            valid = false
                        }
                    }

                    ngModel.$setValidity('date', valid);
                    return valid ? newViewValue : undefined;
                });
            }
        };
    })
    .directive('adPercentage', function () {
        // 货币校验
        return {
            restrict: 'A',
            require: 'ngModel',
            link: function (scope, elm, attrs, ngModel) {
                var REGEXP = /^[0-9]{1,3}(.[0-9]{1,2})?$/;
                ngModel.$parsers.unshift(function (viewValue) {
                    var valid = false;
                    var value = undefined;
                    if (ngModel.$isEmpty(viewValue)) {
                        valid = true;
                    } else {
                        valid = REGEXP.test(viewValue);
                        if (valid) {
                            value = parseFloat(viewValue);
                            if (value < 0 || value > 100) {
                                valid = false;
                                value = undefined;
                            }
                        }
                    }
                    ngModel.$setValidity('percentage', valid);
                    return value;
                });
            }
        };
    })
    .directive('adPolicyRef', function () {
        // 货币校验
        return {
            restrict: 'A',
            require: 'ngModel',
            link: function (scope, elm, attr, ngModel) {
                ngModel.$parsers.unshift(function (viewValue) {
                    if (ngModel.$isEmpty(viewValue)) {
                        return undefined;
                    } else {
                        var newViewValue = '00000000000' + viewValue;
                        newViewValue = newViewValue.substr(newViewValue.length - 12);
                        if (newViewValue == '000000000000') newViewValue = undefined;
                        if (newViewValue != viewValue) {
                            ngModel.$setViewValue(newViewValue);
                            ngModel.$render();
                        }
                        return newViewValue;
                    }
                });
            }
        };
    })
    .directive('adCurrency', function () {
        // 货币校验
        return {
            restrict: 'A',
            require: 'ngModel',
            link: function (scope, elm, attr, ngModel) {
                ngModel.$parsers.unshift(function (viewValue) {
                    if (ngModel.$isEmpty(viewValue)) {
                        ngModel.$setValidity('currency', true);
                        return undefined;
                    } else {
                        var viewValue1 = viewValue;
                        if (isNaN(viewValue1)) {
                            viewValue1 = viewValue1.replace(/[^0-9.]/g, '');
                        }
                        var valid = !isNaN(viewValue1);
                        ngModel.$setValidity('currency', valid);
                        if (valid) {
                            var newViewValue = viewValue1 == '' ? null : parseFloat((Math.floor((parseFloat(viewValue1) * 100).toFixed(1)) / 100).toFixed(2));
                            if (newViewValue != viewValue) {
                                ngModel.$setViewValue(newViewValue);
                                ngModel.$render();
                            }
                        }
                        return valid ? parseFloat(newViewValue) : undefined;
                    }
                });
            }
        };
    })
    .directive('adInteger', function () {
        // 整数校验
        return {
            restrict: 'A',
            require: 'ngModel',
            link: function (scope, elm, attr, ngModel) {
                ngModel.$parsers.unshift(function (viewValue) {
                    if (ngModel.$isEmpty(viewValue)) {
                        ngModel.$setValidity('integer', true);
                        return undefined;
                    } else {
                        var viewValue1 = viewValue;
                        if (isNaN(viewValue1)) {
                            viewValue1 = viewValue1.replace(/[^0-9.]/g, '');
                        }
                        var valid = !isNaN(viewValue1);
                        ngModel.$setValidity('integer', valid);
                        if (valid) {
                            var newViewValue = viewValue1 == '' ? null : parseInt(viewValue1).toString();
                            if (newViewValue !== viewValue) {
                                ngModel.$setViewValue(newViewValue);
                                ngModel.$render();
                            }
                            return parseInt(viewValue);
                        } else {
                            return undefined;
                        }
                    }
                });
            }
        };
    })
    .directive('adDecimal', function () {
        // 整数校验
        return {
            restrict: 'A',
            require: 'ngModel',
            link: function (scope, elm, attr, ngModel) {
                var scale = Math.pow(10, attr.adDecimal);
                ngModel.$parsers.unshift(function (viewValue) {
                    if (ngModel.$isEmpty(viewValue)) {
                        ngModel.$setValidity('decimal', true);
                        return undefined;
                    } else {
                        var viewValue1 = viewValue;
                        if (isNaN(viewValue1)) {
                            viewValue1 = viewValue1.replace(/[^0-9.]/g, '');
                        }
                        var valid = !isNaN(viewValue1);
                        ngModel.$setValidity('decimal', valid);
                        if (valid) {
                            var newViewValue = viewValue1 == '' ? null : parseFloat((Math.floor((parseFloat(viewValue1) * scale).toFixed(1)) / scale).toFixed(attr.adDecimal));
                            if (newViewValue != viewValue) {
                                ngModel.$setViewValue(newViewValue);
                                ngModel.$render();
                            }
                        }
                    }
                    return valid ? newViewValue : undefined;
                });
            }
        };
    })
    .directive('adPattern', function () {
        // 动态pattern
        return {
            restrict: 'A',
            require: 'ngModel',
            link: function (scope, elm, attr, ngModel) {
                var adPattern = attr.adPattern;
                if (adPattern == null || adPattern == '') return;
                var setValidity = function (scope) {
                    var pattern = eval(scope.$eval(adPattern));
                    var valid = ngModel.$modelValue === "" || pattern === "" || ngModel.$modelValue == null || pattern == null || typeof (ngModel.$modelValue) == "undefined" || typeof (pattern) == "undefined" || pattern.test(ngModel.$modelValue);
                    ngModel.$setValidity('pattern', valid);
                };

                // 监控自己
                scope.$watch(attr.ngModel, function (newValue, oldValue, scope) { setValidity(scope); });
                // 监控被相等的值
                scope.$watch(attr.adPattern, function (newValue, oldValue, scope) { setValidity(scope); });
            }
        };
    })
    .directive('adMin', [function () {
        // 最小值校验
        return {
            restrict: 'A',
            require: "ngModel",
            link: function (scope, element, attr, ngModel) {
                var adMin = attr.adMin;
                if (adMin == null || adMin == '') return;
                var isDate = attr.adDate != null;
                var setValidity = function (scope) {
                    var minVal = scope.$eval(adMin);
                    var valid = ngModel.$modelValue === "" || minVal === "" || ngModel.$modelValue == null || minVal == null || typeof (ngModel.$modelValue) == "undefined" || typeof (minVal) == "undefined";
                    if (!valid) {
                        if (isDate) {
                            var modelDate = new Date(ngModel.$modelValue.replace(/-/g,'/'));
                            var minDate = new Date(scope.$eval(adMin).replace(/-/g,'/'));
                            valid = modelDate >= minDate;
                        } else {
                            valid = ngModel.$modelValue >= minVal;
                        }
                    }
                    ngModel.$setValidity('min', valid);
                };

                // 监控自己
                scope.$watch(attr.ngModel, function (newValue, oldValue, scope) { setValidity(scope); });
                // 监控被相等的值
                scope.$watch(attr.adMin, function (newValue, oldValue, scope) { setValidity(scope); });
            }
        };
    }])
    .directive('adMax', [function () {
        // 最大值校验
        return {
            restrict: 'A',
            require: "ngModel",
            link: function (scope, element, attr, ngModel) {
                var adMax = attr.adMax;
                if (adMax == null || adMax == '') return;
                var isDate = attr.adDate != null;
                var setValidity = function (scope) {
                    var maxVal = scope.$eval(adMax);
                    var valid = ngModel.$modelValue === "" || maxVal === "" || ngModel.$modelValue == null || maxVal == null || typeof (ngModel.$modelValue) == "undefined" || typeof (maxVal) == "undefined";
                    if (!valid) {
                        if (isDate) {
                            var modelDate = new Date(ngModel.$modelValue.replace(/-/g, '/'));
                            var maxDate = new Date(scope.$eval(adMax).replace(/-/g, '/'));
                            valid = modelDate <= maxDate;
                        } else {
                            valid = ngModel.$modelValue <= maxVal;
                        }
                    }
                    ngModel.$setValidity('max', valid);
                };

                // 监控自己
                scope.$watch(attr.ngModel, function (newValue, oldValue, scope) { setValidity(scope); });
                // 监控被相等的值
                scope.$watch(attr.adMax, function (newValue, oldValue, scope) { setValidity(scope); });
            }
        };
    }])
    .directive('adUnit', [function () {
        // 金额倍数校验
        return {
            restrict: 'A',
            require: "ngModel",
            link: function (scope, element, attr, ngModel) {
                var adUnit = attr.adUnit;
                if (adUnit == null || adUnit == '') return;
                var setValidity = function (scope) {
                    var unitVal = scope.$eval(adUnit);
                    var valid = isNaN(ngModel.$modelValue) || isNaN(unitVal) || ngModel.$modelValue == null || unitVal == null || ngModel.$modelValue % unitVal == 0;
                    ngModel.$setValidity('unit', valid);
                };

                // 监控自己
                scope.$watch(attr.ngModel, function (newValue, oldValue, scope) { setValidity(scope); });
                // 监控被相等的值
                scope.$watch(attr.adMax, function (newValue, oldValue, scope) { setValidity(scope); });
            }
        };
    }])
    .directive('adNotEqual', [function () {
        // 不等校验
        return {
            restrict: 'A',
            require: "ngModel",
            link: function (scope, element, attr, ngModel) {
                var adNotEqual = attr.adNotEqual;
                if (adNotEqual == null || adNotEqual == '') return;
                var setValidity = function (scope) {
                    var notEqualVal = scope.$eval(adNotEqual);
                    var valid = ngModel.$modelValue === "" || notEqualVal === "" || ngModel.$modelValue == null || notEqualVal == null || typeof (ngModel.$modelValue) == "undefined" || typeof (notEqualVal) == "undefined" || ngModel.$modelValue != notEqualVal;
                    ngModel.$setValidity('notEqual', valid);
                };

                // 监控自己
                scope.$watch(attr.ngModel, function (newValue, oldValue, scope) { setValidity(scope); });
                // 监控被相等的值
                scope.$watch(attr.adNotEqual, function (newValue, oldValue, scope) { setValidity(scope); });
            }
        };
    }]).directive('adEqual', [function () {
        // 相等校验
        return {
            restrict: 'A',
            require: "ngModel",
            link: function (scope, element, attr, ngModel) {
                var adEqual = attr.adEqual;
                if (adEqual == null || adEqual == '') return;
                var setValidity = function (scope) {
                    var equalVal = scope.$eval(adEqual);
                    var valid = ngModel.$modelValue === "" || equalVal === "" || ngModel.$modelValue == null || equalVal == null || typeof (ngModel.$modelValue) == "undefined" || typeof (equalVal) == "undefined" || ngModel.$modelValue == equalVal;
                    ngModel.$setValidity('equal', valid);
                };

                // 监控自己
                scope.$watch(attr.ngModel, function (newValue, oldValue, scope) { setValidity(scope); });
                // 监控被相等的值
                scope.$watch(attr.adEqual, function (newValue, oldValue, scope) { setValidity(scope); });
            }
        };
    }]).directive('adEqualIgnoreCase', [function () {
        // 相等校验，忽视大小写
        return {
            restrict: 'A',
            require: "ngModel",
            link: function (scope, element, attr, ngModel) {
                var adEqualIgnoreCase = attr.adEqualIgnoreCase;
                if (adEqualIgnoreCase == null || adEqualIgnoreCase == '') return;
                var setValidity = function (scope) {
                    var equalVal = scope.$eval(adEqualIgnoreCase);
                    var valid = ngModel.$modelValue === "" || equalVal === "" || ngModel.$modelValue == null || equalVal == null || typeof (ngModel.$modelValue) == "undefined" || typeof (equalVal) == "undefined" || ngModel.$modelValue.toLowerCase() == equalVal.toLowerCase();
                    ngModel.$setValidity('equalIgnoreCase', valid);
                };

                // 监控自己
                scope.$watch(attr.ngModel, function (newValue, oldValue, scope) { setValidity(scope); });
                // 监控被相等的值
                scope.$watch(attr.adEqualIgnoreCase, function (newValue, oldValue, scope) { setValidity(scope); });
            }
        };
    }]);



